package com.jy.adscm.operation.newhscrb.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.newhscrb.bean.NhcMaterialDetail;
import com.jy.adscm.operation.newhscrb.bean.QueryNhcMaterialDetail;
import com.jy.adscm.operation.newhscrb.service.NhcMaterialDetailService;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zyl
 * @since 2020-06-15
 */
@RestController
@RequestMapping("/adscm/operation/nhcMaterialDetail")
public class NhcMaterialDetailController {
    @Autowired
    RedisTemplate<Object, Token> tokenRedisTemplate;
    @Autowired
    NhcMaterialDetailService nhcMaterialDetailService;

    @RequestMapping("insert")
    public SimpleCoreResponse insert(@RequestBody NhcMaterialDetail nhcMaterialDetail, HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        nhcMaterialDetail.setCreateTime(new Date());
        nhcMaterialDetail.setCreator(token.getUser().getId().toString());
        NhcMaterialDetail nhcMaterialDetailnew = nhcMaterialDetailService.insertnhcMaterialDetail(nhcMaterialDetail);
        nhcMaterialDetailService.printCode(nhcMaterialDetailnew.getCode());
        return new SimpleCoreResponse("200","新增成功",true);
    }
    @RequestMapping("update")
    public SimpleCoreResponse update(@RequestBody NhcMaterialDetail nhcMaterialDetail, HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        boolean insert = nhcMaterialDetailService.updateNhcMaterialDetail(token,  nhcMaterialDetail);
        return new SimpleCoreResponse("200","修改成功",insert);
    }
    @RequestMapping("selectByPid")
    public SimpleCoreResponse selectByPid(@RequestBody QueryNhcMaterialDetail queryNhcMaterialDetail, HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        Page<NhcMaterialDetail> page = nhcMaterialDetailService.selectByPid(queryNhcMaterialDetail);
        return new SimpleCoreResponse("200","查询成功",page);
    }
    @RequestMapping("getMaxCode")
    public SimpleCoreResponse getMaxCode( HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        String code  = nhcMaterialDetailService.getMaxCode();
        return new SimpleCoreResponse("200","查询成功",code);
    }

    @RequestMapping("outPutMaterial/{code}/{result}")
    public SimpleCoreResponse outPutMaterial(@PathVariable String code, @PathVariable Integer result, HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        Boolean info= nhcMaterialDetailService.outPutMaterial(code,result,token);
        return new SimpleCoreResponse("200","查询成功",info);
    }
}

